import pygame, os, sys

class Button(pygame.sprite.Sprite):
	def __init__(self, imgName, x, y):
		pygame.sprite.Sprite.__init__(self)
		self.image, self.rect = load_image(imgName)
		self.x = x
		self.y = y
		self.rect.topleft = (x,y)

	def checkClick(self, clickPos):
		clickX = clickPos[0]
		clickY = clickPos[1]
		spriteX = self.rect.left
		spriteY = self.rect.top
		spriteWidth = self.rect.width
		spriteHeight = self.rect.height
		
		if clickX >= spriteX and clickX <= (spriteX + spriteWidth) and\
		       clickY >= spriteY and clickY <= (spriteY + spriteHeight):
			return self
		else:
			return None

def load_image(name, colorkey=None):
	fullname= os.path.join('images', name)
	try:
		image = pygame.image.load(fullname)
	except pygame.error, message:
		print 'Cannot load image:', fullname
		raise SystemExit, message
	image = image.convert()
	if colorkey is not None:
		if colorkey is -1:
			colorkey = image.get_at((0,0))
			image.set_colorkey(colorkey, RLEACCEL)
	return image, image.get_rect()
